package org.trebert.web;
/**
 * 
 */


import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.Random;
/**
 * @author pascal trebert
 *
 */
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = -4893179780142447164L;
	
	public final int MAX = 11;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// hole ServletContext fuer Log (oder aus der Session) ???
		ServletContext scontext = this.getServletContext();
		PrintWriter out = response.getWriter();
		
		// generiere 2 Zufallszahlen
		Random random = new Random();
		int a = random.nextInt(MAX); // exklusive
		int b = random.nextInt(MAX);
		
		// berrechne Ergebnis und merke es in session
		int correctResult = a + b;
		
		// session holen
		HttpSession session = request.getSession();
		session.setAttribute("correctResult", correctResult);
		
		
		scontext.log("bin im AddServlet");
		out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
		out.print("<html>\n");
		out.print("<head>\n");
		out.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
		out.print("<title>Webanwendung -kopfrechner- AddServlet</title>\n");
		out.print("</head>\n");
		out.print("<body>\n");
		out.print("Bitte L&ouml;se folgende Additionsaufgabe:<br />\n");
		out.print(a+ " + " +b+ "<br />\n");
		out.print("<form action='/kopfrechner' method='post'>\n");
		out.print("<input type='text' name='userAnswer' size='10' maxlength='10' <br />\n");
		out.print("<input type='submit' name='ok' value='OK'><br />\n");
		out.print("</form >\n");

		out.print("</body>\n");
		out.print("</html>\n");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doPost(request, response);
	}
	
}
